package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.domain.*;
import cn.wolfcode.wolf2w.business.query.SearchQuery;
import cn.wolfcode.wolf2w.business.service.ISearchService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.member.api.domain.UserInfo;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/search")
@RestController
public class SearchController {
    @Autowired
    private ISearchService searchService;
    /*将mysql中的攻略保存到es中*/
    @PostMapping("/initStrategyEs")
     public R<?> initStrategyEs(@RequestBody StrategyES strategyES){
        //保存攻略
        searchService.saveStrategy(strategyES);
        return R.ok();
    }
    @PostMapping("/initUserEs")
    R<?>initStrategyEs(@RequestBody UserES userES){
        searchService.saveUserES(userES);
        return R.ok();
    }
    @PostMapping("/initNoteEs")
    R<?>initNoteEs(@RequestBody NoteES noteES){
        searchService.saveNoteEs(noteES);
        return R.ok();
    }
    @PostMapping("/initDestinationEs")
    R<?>initNoteEs(@RequestBody DestinationES destinationES){
        searchService.saveDestinationEs(destinationES);
        return R.ok();
    }
    /*查询*/
    @GetMapping
    public R<?> search(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        //请求在一起，但是具体处理不在一起
        //根据qo中传递的type条件进行分别查询
        switch (qo.getType()){
            case 1:
                return R.ok(searchStrategy(qo));
            case 2:
                return R.ok(searchNote(qo));
            case 3:
                return R.ok(searchUser(qo));
            case 4:
                return R.ok(searchAnswer(qo));
                case 0:
                    return R.ok(searchDestination(qo));
            default:
                return R.ok(searchAll(qo));
        }

    }
    /*查询目的地目的地*/
    private Object searchDestination(SearchQuery qo) throws InvocationTargetException, IllegalAccessException{
        return searchService.searchWithHighLight(Destination.class, DestinationES.class, qo, "name", "info");

    }

    /*查询所有*/
    private Map<String, Object> searchAll(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        Page<UserInfo> userInfoPage = searchService.searchWithHighLight(UserInfo.class, UserES.class, qo, "nickname", "info", "city");
        Page<Note> notePage = searchService.searchWithHighLight(Note.class, NoteES.class, qo, "title", "summary");
        Page<Strategy> strategyPage = searchService.searchWithHighLight(Strategy.class, StrategyES.class, qo, "title", "subTitle","summary");
        Page<Destination> destinationPage = searchService.searchWithHighLight(Destination.class, DestinationES.class, qo, "name", "info");
        long total = userInfoPage.getTotalElements() + notePage.getTotalElements() + strategyPage.getTotalElements() + destinationPage.getTotalElements();
        List<UserInfo> users = userInfoPage.getContent();
        List<Note> notes = notePage.getContent();
        List<Strategy> strategies = strategyPage.getContent();
        List<Destination> dests = destinationPage.getContent();
        Map<String,Object> result = new HashMap<>();
        result.put("total",total);
        result.put("users",users);
        result.put("notes",notes);
        result.put("strategies",strategies);
        result.put("dests",dests);
        return result;

    }
    /*查询问答*/
    private Object searchAnswer(SearchQuery qo) {
        return null;

    }
    /*查询用户*/
    private Object searchUser(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        return searchService.searchWithHighLight(UserInfo.class, UserES.class, qo, "nickname", "info", "city");

    }
    /*查询游记*/
    private Object searchNote(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {
        return searchService.searchWithHighLight(Note.class, NoteES.class, qo, "title", "summary");

    }
    /*查询攻略*/
    private Object searchStrategy(SearchQuery qo) throws InvocationTargetException, IllegalAccessException {

        return searchService.searchWithHighLight(Strategy.class, StrategyES.class, qo, "title", "subTitle","summary");

    }

}
